package com.fenger.sys.controller;

import com.fenger.sys.contast.SysConstast;
import com.fenger.sys.domain.Permission;
import com.fenger.sys.domain.User;
import com.fenger.sys.service.PermissionService;
import com.fenger.sys.utils.*;
import com.fenger.sys.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class MenuController {

    private final static String URL_MENU = "menu/";


    @Autowired
    PermissionService permissionService;

    /**
     * 加载主菜单的树json
     * @param permissionVo
     * @return
     */
    @ResponseBody
    @RequestMapping(URL_MENU+"loadAllLeftMenuTree")
    public List<TreeNode> loadAllLeftMenuTree(PermissionVo permissionVo){
        HttpSession session = WebUtils.getCurrentSession();
        permissionVo.setAvailable(SysConstast.AVAILABLE_TRUE);
        permissionVo.setType(SysConstast.TYPE_PERMISSION_MENU);
        User user = (User) session.getAttribute("user");
        List<Permission> permissions = null;
        if(user.getType()==SysConstast.USER_SUPER){
            permissions = permissionService.queryAllPermission(permissionVo);
        }else {
            permissions=permissionService.queryPermissionByUserid(permissionVo,user.getId());
        }
        List<TreeNode> treeNodes = new ArrayList<>();
        for (Permission permission : permissions) {
            Integer id = permission.getId();
            String title = permission.getName();
            String href = permission.getHref();
            String icon = permission.getIcon();
            Boolean spread = permission.getOpen()==SysConstast.STATUS_OPEN_TRUE?true:false;
            Integer pId = permission.getPid();
            treeNodes.add(new TreeNode(id, title, href, icon, spread, pId));
        }

        return TreeNodeUtils.build(treeNodes,1);
    }
    @ResponseBody
    @RequestMapping(URL_MENU+"loadAllLeftMenuManagerTree")
    public DtreeJson loadAllLeftMenuManagerTree(PermissionVo permissionVo){
        HttpSession session = WebUtils.getCurrentSession();
        permissionVo.setAvailable(SysConstast.AVAILABLE_TRUE);
        permissionVo.setType(SysConstast.TYPE_PERMISSION_MENU);
        User user = (User) session.getAttribute("user");
        List<Permission> permissions = null;
        if(user.getType()==SysConstast.USER_SUPER){
            permissions = permissionService.queryAllPermission(permissionVo);
        }else {
            permissions=permissionService.queryPermissionByUserid(permissionVo,user.getId());
        }



        List<DtreeNode> dtreeNodes = new ArrayList<>();
        for (Permission permission : permissions) {
            Integer id =permission.getId();
            String title = permission.getName();
            String checkArr = "0";
            Integer parentId = permission.getPid();
            Boolean spread = permission.getOpen()==SysConstast.STATUS_OPEN_TRUE?true:false;
            String iconClass = permission.getIcon();
            dtreeNodes.add(new DtreeNode(id,title,checkArr,parentId,spread,iconClass));
        }
        List<DtreeNode> nodes = DtreeNodeUtils.build(dtreeNodes, 0);
        return new DtreeJson(SysConstast.SUCCESS_CODE,SysConstast.SUCCESS_MSG,nodes);
    }


    @ResponseBody
    @RequestMapping(URL_MENU+"loadXlTreePermissionAndMenu")
    public DtreeJson loadXlTreePermissionAndMenu(PermissionVo permissionVo){
        HttpSession session = WebUtils.getCurrentSession();
        permissionVo.setAvailable(SysConstast.AVAILABLE_TRUE);
        User user = (User) session.getAttribute("user");
        List<Permission>  permissions = permissionService.queryAllPermission(permissionVo);

        List<DtreeNode> dtreeNodes = new ArrayList<>();
        for (Permission permission : permissions) {
            Integer id =permission.getId();
            String title = permission.getName();
            String checkArr = "0";
            Integer parentId = permission.getPid();
            Boolean spread = permission.getOpen()==SysConstast.STATUS_OPEN_TRUE?true:false;
            String iconClass = permission.getIcon();
            dtreeNodes.add(new DtreeNode(id,title,checkArr,parentId,spread,iconClass));
        }
        List<DtreeNode> nodes = DtreeNodeUtils.build(dtreeNodes, 0);
        return new DtreeJson(SysConstast.SUCCESS_CODE,SysConstast.SUCCESS_MSG,nodes);
    }
    @ResponseBody
    @RequestMapping(URL_MENU+"loadAllMenuForTable")
    public DataGirdView loadAllMenuForTable(PermissionVo permissionVo){
        permissionVo.setType(SysConstast.TYPE_PERMISSION_MENU);
        return this.permissionService.queryAllPermissionForPage(permissionVo);
    }



    /**
     * 权限的全查询
     * @param permissionVo
     * @return
     */
    @ResponseBody
    @RequestMapping(URL_MENU+"loadAllPermissionForTable")
    public DataGirdView loadAllPermissionForTable(PermissionVo permissionVo){
        permissionVo.setType(SysConstast.TYPE_PERMISSION_PERMISSION);
        return this.permissionService.queryAllPermissionForPage(permissionVo);
    }
    @ResponseBody
    @RequestMapping(URL_MENU+"loadMaxOrderNum")
    public Integer loadMaxOrderNum(){
        return this.permissionService.queryMaxOrderNum();
    }

    @ResponseBody
    @RequestMapping(URL_MENU + "addMenu")
    private DataGirdView addMenu(PermissionVo permissionVo) {
        permissionVo.setType(SysConstast.TYPE_PERMISSION_MENU);
        HttpSession session = WebUtils.getCurrentSession();
        try {
            this.permissionService.addPermission(permissionVo);
            return new DataGirdView(SysConstast.ADD_SUCCESS_CODE, SysConstast.ADD_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.ADD_ERROR_CODE, SysConstast.ADD_ERROR_MSG, 0L, null);
        }
    }

    @ResponseBody
    @RequestMapping(URL_MENU + "updateMenu")
    private DataGirdView updateMenu(PermissionVo permissionVo) {
        try {
            this.permissionService.updatePermission(permissionVo);
            return new DataGirdView(SysConstast.UPDATE_SUCCESS_CODE, SysConstast.UPDATE_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.UPDATE_ERROR_CODE, SysConstast.UPDATE_ERROR_MSG, 0L, null);
        }
    }

    @ResponseBody
    @RequestMapping(URL_MENU + "deleteMenu")
    private DataGirdView deleteMenu(PermissionVo permissionVo) {
        try {
            this.permissionService.deletePermission(permissionVo.getId());
            return new DataGirdView(SysConstast.DELETE_SUCCESS_CODE, SysConstast.DELETE_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.DELETE_ERROR_CODE, SysConstast.DELETE_ERROR_MSG, 0L, null);
        }
    }

    @ResponseBody
    @RequestMapping(URL_MENU + "addPermission")
    private DataGirdView addPermission(PermissionVo permissionVo) {
        permissionVo.setType(SysConstast.TYPE_PERMISSION_PERMISSION);
        HttpSession session = WebUtils.getCurrentSession();
        try {
            this.permissionService.addPermission(permissionVo);
            return new DataGirdView(SysConstast.ADD_SUCCESS_CODE, SysConstast.ADD_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.ADD_ERROR_CODE, SysConstast.ADD_ERROR_MSG, 0L, null);
        }
    }

    @ResponseBody
    @RequestMapping(URL_MENU + "updatePermission")
    private DataGirdView updatePermission(PermissionVo permissionVo) {
        try {
            this.permissionService.updatePermission(permissionVo);
            return new DataGirdView(SysConstast.UPDATE_SUCCESS_CODE, SysConstast.UPDATE_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.UPDATE_ERROR_CODE, SysConstast.UPDATE_ERROR_MSG, 0L, null);
        }
    }

    @ResponseBody
    @RequestMapping(URL_MENU + "deletePermission")
    private DataGirdView deletePermission(PermissionVo permissionVo) {
        try {
            this.permissionService.deletePermission(permissionVo.getId());
            return new DataGirdView(SysConstast.DELETE_SUCCESS_CODE, SysConstast.DELETE_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.DELETE_ERROR_CODE, SysConstast.DELETE_ERROR_MSG, 0L, null);
        }
    }


    @ResponseBody
    @RequestMapping(URL_MENU + "checkHasChildren")
    private DataGirdView checkHasChildren(PermissionVo permissionVo) {
        try {
            Boolean hasChildren = this.permissionService.checkHasChildren(permissionVo.getId());
            if(hasChildren){
                return new DataGirdView(SysConstast.HAS_CHILDREN_CODE, null, 1L, null);
            }
            return new DataGirdView(SysConstast.NOT_HAS_CHILDREN_CODE, null, 0L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.NOT_HAS_CHILDREN_CODE, null, 0L, null);
        }
    }


    @ResponseBody
    @RequestMapping(URL_MENU + "deleteListMenu")
    private DataGirdView deleteListMenu(@RequestParam(value="id[]", required = false) String[] id) {
        for (int i = 0; i < id.length; i++) {
            String s = id[i];
            System.out.println(s);
        }
        return new DataGirdView(SysConstast.DELETE_ERROR_CODE, SysConstast.DELETE_ERROR_MSG, 0L, null);
    }

}
